//
//  ToolbarCallbackDecorator.mm
//  XPComDemo
//
//  Created by Junjie Gong on 12-5-19.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#include "ToolbarCallbackDecorator.h"
#include "xpcom-config.h"

#include "nsStringAPI.h"
#include "nsIWidget.h"
#include "nsIWebBrowserPersist.h"
#include "nsILocalFile.h"
#include "nsComponentManagerUtils.h"
#include "nsIWindowWatcher.h"
#include "nsIDOMWindow.h"
#include "nsIDOMDocument.h"
#include "nsIDOMLocation.h"
#include "nsISimpleEnumerator.h"
#import "MainWindowDelegate.h"



ToolbarCallbackDecorator::ToolbarCallbackDecorator(){
    toolbarCallback = NULL;
    windowDelegate = NULL;
}

int ToolbarCallbackDecorator::setCallback(nsToolbarCallback *callback){

    toolbarCallback = callback;
    

    
    return 0;
}


void* ToolbarCallbackDecorator::GetBrowserWindow(){
    
    if (toolbarCallback) {
        nsIBaseWindow *window;
        GetTargetWindow(&window);
        
        nsIWidget* mainWidget;
        window->GetMainWidget(&mainWidget);
        void* nswindow = mainWidget->GetNativeData(NS_NATIVE_WINDOW);
        return nswindow;
    }
    
    return NULL;

}

NS_IMETHODIMP ToolbarCallbackDecorator::SentOpeCode2Toolbar(PRInt32 opId, PRInt32 opCode){
    return NS_ERROR_NOT_IMPLEMENTED;
}

NS_IMETHODIMP ToolbarCallbackDecorator::GetTargetWindow(nsIBaseWindow * *aTargetWindow){
    
    if (aTargetWindow!=NULL && toolbarCallback) {
        nsString agent;        
        toolbarCallback->GetUserAgent(agent);
        const unsigned short* buff = agent.get();
        NSString *s = [NSString stringWithCharacters:buff length:agent.Length()];
        NSLog(@"%@", s);
        NSUInteger len = s.length;
        const char* bf = [s UTF8String];
//        int len2 = strlen(bf);
        //toolbarCallback->SentOpeCode2Toolbar(1,1);
        return toolbarCallback->GetTargetWindow(aTargetWindow);
    }
    
    return NS_ERROR_NOT_IMPLEMENTED;
}

NS_IMETHODIMP ToolbarCallbackDecorator::SetTargetWindow(nsIBaseWindow *aTargetWindow){
    return NS_ERROR_NOT_IMPLEMENTED;
}

NS_IMETHODIMP ToolbarCallbackDecorator::GetUserAgent(nsAString & aUserAgent){
    return NS_ERROR_NOT_IMPLEMENTED;
}

NS_IMETHODIMP ToolbarCallbackDecorator::SetUserAgent(const nsAString & aUserAgent){
    return NS_ERROR_NOT_IMPLEMENTED;
}



